package com.example.mycarcalling.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.lib_cache.service.ApiService;
import com.example.lib_cache.viewmodel.EditActivityViewModel;
import com.example.lib_common.util.LiveDataManager;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityEditBinding;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class EditActivity extends AppCompatActivity {

    private final static String TAG = EditActivity.class.getSimpleName();

    private ActivityEditBinding activityEditBinding = null;
    private EditActivityViewModel editActivityViewModel = null;

    private Context context = null;

    @Inject
    ApiService apiService;

    private String request;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityEditBinding = DataBindingUtil
                .setContentView(this, R.layout.activity_edit);
        context = this;
        request = getIntent().getStringExtra("request");
        initViewModel();
        if("phone".equals(request)){
            setTitle("修改手机号");
            initPhoneView();
            initPhoneData();
            initPhoneListener();
            initObserve();
        }
        if("nickName".equals(request)){
            setTitle("修改昵称");
            initNickNameView();
            initNickNameData();
            initNickNameListener();
            initObserve();
        }
    }

    private void initObserve() {
        editActivityViewModel.getResult().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    Toast.makeText(context, "修改成功", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(context, "修改失败！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initViewModel(){
        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
        editActivityViewModel = viewModelProvider.get(EditActivityViewModel.class);
    }


    private void initPhoneView(){
        activityEditBinding.tietInfoEt.setInputType(InputType.TYPE_CLASS_PHONE);
    }

    private void initNickNameView(){
    }

    private void initPhoneData(){

        activityEditBinding.tietInfoEt.setText(
                LiveDataManager.getInstance().with("phone",String.class).getValue());
    }

    private void initNickNameData(){

        activityEditBinding.tietInfoEt.setText(
                LiveDataManager.getInstance().with("nickname",String.class).getValue());
    }

    private void initPhoneListener(){

        activityEditBinding.tietInfoEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityEditBinding.tietInfoEt.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditBinding.tietInfoEt.setError("请输入手机号");
                }else {
                    if(str.length()==11){
                        editActivityViewModel.setUpdateInfo(str);
                    }else {
                        activityEditBinding.tietInfoEt.setError("请输入规范的手机号码");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityEditBinding.tietInfoEt.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditBinding.tietInfoEt.setError("请输入手机号");
                }else {
                    if(str.length()==11){
                        editActivityViewModel.setUpdateInfo(str);
                    }else {
                        activityEditBinding.tietInfoEt.setError("请输入规范的手机号码");
                    }
                }
            }
        });

        //提交按钮
        activityEditBinding.btnConfirmEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editActivityViewModel.requestChangePhone(apiService,context);
            }
        });
    }

    private void initNickNameListener(){

        activityEditBinding.tietInfoEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityEditBinding.tietInfoEt.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditBinding.tietInfoEt.setError("请输入昵称");

                }else {
                    if(str.length()>=2&&str.length()<=10){
                        editActivityViewModel.setUpdateInfo(str);
                    }else {
                        activityEditBinding.tietInfoEt.setError("请输入2到10长度的昵称");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityEditBinding.tietInfoEt.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditBinding.tietInfoEt.setError("请输入昵称");

                }else {
                    if(str.length()>=2&&str.length()<=10){
                        editActivityViewModel.setUpdateInfo(str);
                    }else {
                        activityEditBinding.tietInfoEt.setError("请输入2到10长度的昵称");
                    }
                }
            }
        });
        //提交按钮
        activityEditBinding.btnConfirmEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editActivityViewModel.requestChangeNickName(apiService,context);
            }
        });
    }
}